const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const config = require('./config/');
const vuxLoader = require('vux-loader');
const IS_ENV = process.env.NODE_ENV == 'production';

var plugins = [];
if (IS_ENV) { //生产环境
    plugins.push(new webpack.DefinePlugin({
        'process.env': { //设置成生产环境
            NODE_ENV: '"production"'
        }
    }));
    plugins.push(new webpack.optimize.UglifyJsPlugin({ //压缩代码
        mangle: true,
        compress: {
            warnings: false,
            drop_console:true,
            pure_funcs:['console.log']
        }
    }));
}
plugins.push(
    new webpack.optimize.CommonsChunkPlugin({
        name: 'vendor',
        minChunks: function(module){
            return module.context && module.context.includes('node_modules');
        }
    }),
    new webpack.optimize.CommonsChunkPlugin({
        name: 'manifest',
        minChunks: Infinity
    }),
);

plugins.push(
    new HtmlWebpackPlugin({ //根据模板插入css/js等生成最终HTML
        filename: './index.html', //生成的html存放路径，相对于 path
        template: './src/template/index.html', //html模板路径
    })
);

const webpackConfig = {
    entry: ['./src/main.js'], //编译入口文件
    output: {
        publicPath: config.publicPath, //服务器的路径
        path: IS_ENV ? '../plp-h5-2' : path.resolve(__dirname + config.publicPath), //生产环境
        // path: IS_ENV ? '../../SmallHoney2-H5-build-test' : path.resolve(__dirname + config.publicPath), //测试环境
        filename: '[name].js' //编译后的文件名
    },
    module: {
        loaders: [
            {
                test: /\.js(x)*$/,
                exclude: /^node_modules$/,
                loader: 'babel',
            },
            {
                test: /\.vue$/,
                loader: 'vue'
            },
            {
                test: /\.css/,
                exclude: /^node_modules$/,
                loader: 'style-loader?-autoprefixer!css-loader?-autoprefixer!autoprefixer-loader?{ browsers: [\'last 100 versions\'] }'
            },
            {
                test: /\.less$/,
                // exclude: /^node_modules$/,
                loader: 'style-loader?-autoprefixer!css-loader?-autoprefixer!autoprefixer-loader?{ browsers: [\'last 100 versions\'] }!less-loader'
            },
            {
                test: /\.(png|jpg|gif)$/,
                exclude: /^node_modules$/,
                loader: 'url?limit=2000&name=images/[name].[ext]' //注意后面那个limit的参数，当你图片大小小于这个限制的时候，会自动启用base64编码图片
            },
            {
                test: /\.(eot|woff|svg|ttf|woff2|appcache)(\?|$)/,
                exclude: /^node_modules$/,
                loader: 'file-loader?name=[name].[ext]'
            },
            {
                test : /\.(mp3)(\?.*)?$/,
                loader : 'file-loader?name=audio/[name].[ext]'
            },
            {
                test : /\.(mp4)(\?.*)?$/,
                loader : 'file-loader?name=video/[name].[ext]'
            }
        ]
    },
    plugins,
    resolve: {
        extensions: ['', '.js', '.vue', '.jsx'], //后缀名自动补全
        alias: {
            vue: 'vue/dist/vue.js', //webpack打包时，需要设置别名
            store: path.resolve('src/store/'), //常用工具方法
            '@': path.resolve('src/pages')   // 页面路径
        }
    },
    vue: {
        postcss: [
            require('autoprefixer')({
                browsers: ['last 100 versions']
            })
        ]
    },
    externals: {
        'AMap': 'AMap',
        'vue': 'Vue',
        'vuex': 'Vuex',
        'vue-router': 'VueRouter',
        'vue-clipboard2': 'VueClipboard',
        'vue-lazyload': 'VueLazyLoad',
        'axios': 'axios',
        'vue-scroller': 'VueScroller',
        'better-scroll': 'BScroll',
        'swiper': 'Swiper',
        'vue-awesome-swiper': 'VueAwesomeSwiper',
    },
};
module.exports = vuxLoader.merge(webpackConfig, {
    plugins: ['vux-ui']
});